package com.d3works.apps.production.web;

import com.d3works.apps.base.db.AppProduction;
import com.d3works.apps.base.db.repo.AppProductionRepository;
import com.d3works.apps.production.service.app.AppProductionService;
import com.d3works.exception.service.HandleExceptionResponse;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class AppProductionController {

  public void generateProductionConfigurations(ActionRequest request, ActionResponse response) {

    Beans.get(AppProductionService.class).generateProductionConfigurations();

    response.setReload(true);
  }

  @HandleExceptionResponse
  public void checkIfOutsourcingDisabled(ActionRequest request, ActionResponse response) {
    AppProduction app = request.getContext().asType(AppProduction.class);
    if (app.getId() == null) {
      return;
    }
    boolean outsourcing =
        Beans.get(AppProductionRepository.class).find(app.getId()).getManageOutsourcing();
    boolean outsourcingContxt = app.getManageOutsourcing();
    if (outsourcing && !outsourcingContxt) {
      Beans.get(AppProductionService.class).updatePartnerSubcontractTag();
    }
  }
}
